#include <stdio.h>

int main(int argc, char *argv[]) {
	
	int urn[5] = {100,200,300,400,500};
	int *ptr1, *ptr2, *ptr3;
	
	ptr1 = urn;  		// 把一个地址赋值给指针
	ptr2 = &urn[2];		// 把一个地址赋值给指针
	
	// 解引用指针，以获取指针的地址 &ptr1:获取指针的指针
	printf("pointer value, derferenced pointer, pointer address:\n");
	printf("ptr1 = %p,  *ptr1 = %d,  &ptr1 = %p,  *(&ptr1) = %p\n", ptr1, *ptr1, &ptr1, *(&ptr1));
	
	// 指针加法
	ptr3 = ptr1 + 4;
	printf("\nadding an int to a pointer:\n");
	printf("ptr1 + 4 = %p, *(ptr1 + 4) = %d\n",ptr1 + 4,*(ptr1 + 4));
	
	// 指针递增
	ptr1++;
	printf("\nvalue after ptr1++:\n");
	printf("ptr1 = %p,  *ptr1 = %d,  &ptr1 = %p,  *(&ptr1) = %p\n", ptr1, *ptr1, &ptr1, *(&ptr1));
	
	
	// 指针递减
	ptr2--;
	printf("\nvalue after ptr2--:\n");
	printf("ptr2 = %p,  *ptr2 = %d,  &ptr2 = %p,  *(&ptr2) = %p\n", ptr2, *ptr2, &ptr2, *(&ptr2));
		
	// 恢复初始值
	--ptr1;	
	++ptr2;
	printf("\nPointer reset to origin values:\n");
	printf("ptr1 = %p,  ptr2 = %p\n", ptr1, ptr2);
	
	// 一个指针减去另一个指针
	printf("\nsubtracting one pointer from another:\n");
	printf("ptr2 = %p,  ptr1 = %p, ptr2-ptr1 = %td\n", ptr2, ptr1, ptr2-ptr1);
	
	// 一个指针减去一个整数
	printf("\nsubtracting an int from a pointer:\n");
	printf("ptr2 = %p,  ptr1 = %p, ptr2-2 = %p\n", ptr2, ptr1, ptr2-2);
	
	
}